import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Row, Table, Button, Divider, Popconfirm } from 'antd';
import { formatMessage, FormattedMessage } from 'umi/locale';
import AddProductServiceForm from './components/add';
import EditProductServiceForm from './components/edit';

@connect(({ product, loading }) => ({
  product,
  loading: loading.models.product,
}))
class ProductService extends PureComponent {
  state = {
    modalVisible: false,
    editModalVisible: false,
  };

  componentDidMount() {
    const { dispatch, params, protocolId } = this.props;

    dispatch({
      type: 'product/fetchProductService',
      payload: { productId: params.id },
    });

    dispatch({
      type: 'product/fetchProtocolField',
      payload: protocolId,
    });

    dispatch({
      type: 'product/fetchDataType',
    });
  }

  columns = [
    {
      key: 'name',
      title: formatMessage({ id: 'app.dm.product.service.table.name' }),
      dataIndex: 'name',
      width: 150,
    },
    {
      key: 'label',
      title: formatMessage({ id: 'app.dm.product.service.table.label' }),
      dataIndex: 'label',
      width: 150,
    },

    {
      key: 'desc',
      title: formatMessage({ id: 'app.dm.product.service.table.desc' }),
      dataIndex: 'desc',
      width: 150,
    },
    {
      key: 'createTime',
      title: formatMessage({ id: 'app.dm.product.service.table.createTime' }),
      dataIndex: 'createTime',
      width: 150,
    },
    {
      key: 'action',
      title: formatMessage({ id: 'app.dm.product.service.table.action' }),
      width: 80,
      render: record => (
        <Fragment>
          <a onClick={() => this.editView(record.id)}>
            <FormattedMessage id="app.dm.product.service.table.action.edit" />
          </a>
          <Divider type="vertical" />
          <Popconfirm
            title={
              <div style={{ width: 280 }}>
                <div style={{ fontWeight: 'bold', color: '000', marginBottom: 10 }}>
                  <span>
                    {formatMessage(
                      { id: 'app.dm.product.service.del.confirm' },
                      { name: record.name },
                    )}
                  </span>
                </div>
              </div>
            }
            onConfirm={() => this.handleDelete(record.id)}
          >
            <a>
              <FormattedMessage id="app.dm.product.service.table.action.del" />
            </a>
          </Popconfirm>
        </Fragment>
      ),
    },
  ];

  handleDelete = id => {
    const { dispatch, params } = this.props;
    dispatch({
      type: 'product/delService',
      payload: id,
      params,
    });
  };

  editView = id => {
    const { dispatch } = this.props;
    const { handleEditModalVisible, formRef } = this;
    dispatch({
      type: 'product/fetchServiceById',
      payload: id,
      handleEditModalVisible,
      formRef,
    });
  };

  handleModalVisible = flag => {
    this.setState({
      modalVisible: !!flag,
    });
  };

  handleEditModalVisible = flag => {
    this.setState({
      editModalVisible: !!flag,
    });
  };

  handleAdd = (fieldsValue, paramList, form, clearParamList) => {
    const { dispatch, params } = this.props;
    const { handleModalVisible } = this;
    dispatch({
      type: 'product/addService',
      payload: {
        productId: params.id,
        name: fieldsValue.name,
        label: fieldsValue.label,
        desc: fieldsValue.desc,
        serviceParamList: paramList,
      },
      form,
      handleModalVisible,
      clearParamList,
    });
  };

  handleEdit = (fieldsValue, paramList, form, clearParamList) => {
    const { dispatch } = this.props;
    const { handleEditModalVisible } = this;
    dispatch({
      type: 'product/editService',
      payload: {
        id: fieldsValue.id,
        productId: fieldsValue.productId,
        name: fieldsValue.name,
        label: fieldsValue.label,
        desc: fieldsValue.desc,
        serviceParamList: paramList,
      },
      form,
      handleEditModalVisible,
      clearParamList,
    });
  };

  render() {
    const { modalVisible, editModalVisible } = this.state;
    const {
      product: { services, pagination, serviceDetail },
      loading,
      dispatch,
      params,
    } = this.props;

    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      ...pagination,
      onShowSizeChange(curr, size) {
        dispatch({
          type: 'product/fetchProductService',
          payload: { productId: params.id, ...{ pageNum: curr, pageSize: size } },
        });
      },
      onChange(curr) {
        dispatch({
          type: 'product/fetchProductService',
          payload: {
            productId: params.id,
            ...{ pageNum: curr, pageSize: pagination.currentSize },
          },
        });
      },
    };
    const AddModalProps = {
      handleModalVisible: this.handleModalVisible,
      handleAdd: this.handleAdd,
    };

    const EditModalProps = {
      handleEditModalVisible: this.handleEditModalVisible,
      handleEdit: this.handleEdit,
      detail: serviceDetail,
    };

    return (
      <Fragment>
        <Row style={{ textAlign: 'right' }}>
          <Button onClick={() => this.handleModalVisible(true)} type="primary">
            <FormattedMessage id="app.dm.product.service.add" />
          </Button>
        </Row>
        <Row>
          <Table
            columns={this.columns}
            dataSource={services}
            loading={loading}
            pagination={paginationProps}
            rowKey="id"
            style={{ marginTop: 15 }}
          />
        </Row>
        <AddProductServiceForm {...AddModalProps} modalVisible={modalVisible} />
        <EditProductServiceForm
          {...EditModalProps}
          modalVisible={editModalVisible}
          wrappedComponentRef={ref => {
            this.formRef = ref;
          }}
        />
      </Fragment>
    );
  }
}

export default ProductService;
